<template>
    <div class="upload-file-box">
        <!-- v-if="isShowDialog" -->
        <van-dialog
            :showConfirmButton="false"
            :showCancelButton="true"
            v-model="show"
            @cancel="showTemplate"
            title="处理订单" >
            <van-card>
                <template slot="desc">
                    <div class="file-Dialog">
                        <div style="padding: 10px 0">
                            <span v-if="currentOrderList && currentOrderList.status === '发明人确认定稿中'" class="title">发送定稿文件</span>
                            <span v-else class="title">上传完成文件</span> 
                            <van-uploader 
                                :after-read="afterRead"
                                :accept="'doc/docx'" 
                                :max-count="5"
                                multiple>
                                <template slot="default">
                                    <van-button type="primary" size="mini">上传文件</van-button>
                                </template>
                            </van-uploader>
                        </div>
                        <p class="waring" v-if="currentOrderList && currentOrderList.status === '发明人确认定稿中'">* 文件为文档格式 后缀名为.doc/.docx</p>
                        <p class="waring" v-else >* 文件格式：编号-姓名(发明人)-拟申报名称-申报类型.rar/zip 后缀名为.rar/zip</p>
                    </div>
                </template>
            </van-card>
        </van-dialog>
    </div>
</template>

<script>
import axios from 'axios'
import { Dialog, Toast } from 'vant'

export default {
    data() {
        return {
            show: true,
            baseUrls: global.baseurl
        }
    },
    methods: {
        afterRead(file) {
            // 此时可以自行将文件上传至服务器
            console.log(file);

            let fileName = []
        
            const formData = new FormData()

            if(file.length) {
                // 多文件上传
                file.map((item, i) => {
                    fileName.push(item.file.name)
                    if(i === 0) {
                        formData.append('file', item.file)
                    }else{
                        formData.append(`file${i}`, item.file)
                    }
                })
            }else{
                fileName.push(file.file.name)
                // 单文件上传
                formData.append("file", file.file)
            }

            // 状态为发明人确认定稿中时
            if(this.currentOrderList.status === '发明人确认定稿中') {
                // 任何格式都可以上传
                axios.post(`${baseUrls}/order/${this.currentOrderList.id}/upload`, formData, {
                    headers: {
                        "context-type": "multer/form-data",
                        Authorization: "Bearer " + this.$cookies.get('token')
                    }
                }).then(res => {
                    if(res.status === 200) {
                        Toast('上传成功')
                    }else{
                        Toast('上传错误')
                    }
                })
            }else{
                // * 文件格式：编号-姓名(发明人)-拟申报名称-申报类型.rar/zip 后缀名为.rar/zip
                // 进行格式效验
                let name = `${this.currentOrderList.order_number}-${this.currentOrderList.agent.name}(${this.currentOrderList.user_name})-${this.currentOrderList.patent_name}-${this.currentOrderList.type}`

                let flag = fileName.every(item => item.split('.')[0] === name && (item.split('.')[1] === 'rar' || item.split('.')[1] === 'zip'))

                if(flag) {
                    axios.post(`${baseUrls}/api/order/${this.currentOrderList.id}/upload`, formData, {
                        headers: {
                            "context-type": "multer/form-data",
                            Authorization: "Bearer " + this.$cookies.get('token')
                        }
                    }).then(res => {
                        if(res.status === 200) {
                            Toast('上传成功')
                        }else{
                            Toast('上传错误')
                        }
                    })
                }else{
                    Toast('文件格式上传错误请重新上传')
                }
                
            }
        },
        showTemplate() {
            this.$emit('close')
        },
        cancel() {
            Dialog.close
        }
    },
    props: [
        // 'isShowDialog', // 是否显示上传文件弹出框
        'currentOrderList' // 当前案件信息
    ]

}
</script>

<style lang="less" scoped>
    .upload-file-box{
        .file-Dialog{
            padding: 30px 0 0 0;
 
            p{
                margin: 0;
                padding: 0;
            }
            
            .title,
            .waring,
            .tip{
                font-size: 14px;
                padding: 5px;
            }

            .waring{
                color: red;
            }

        }
    }
</style>